# -*- coding: utf-8 -*-
'''2017-6-3 pylint format'''

# ----------------------------------------------------------------------------
# Use For sys control!
# ----------------------------------------------------------------------------
import sys
import os


def get_pyver():
    '''获取python版本'''
    major_ver = sys.version_info[0]
    return str(major_ver) + "." + str(sys.version_info[1])


def get_lanip():
    '''获取局域网地址'''
    import socket
    tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    tmpsocket.connect(("8.8.8.8", 80))
    addr = tmpsocket.getsockname()[0]
    tmpsocket.close()
    return addr


def get_hostname():
    '''获取电脑名称'''
    import socket
    hostname = socket.getfqdn(socket.gethostname())
    return hostname


def get_macadr():
    '''获取mac地址'''
    import uuid
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])


def is_oninternet(cbcall=0):
    '''检查是否联网'''
    _cb = None
    if hasattr(cbcall, "__call__"):
        _cb = cbcall
    import urllib2
    try:
        urllib2.urlopen("http://www.baidu.com", timeout=2)
    except Exception as neterr:
        if _cb:
            _cb(False)
        print neterr
        return False
    if _cb:
        _cb(True)
    return True


def set_sysenv(key, value):
    '''设置系统变量'''
    os.environ[key] = value


def get_sysenv(key):
    '''获取系统变量'''
    os.getenv(key)
